package org.bzjj.servlet.update;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bzjj.bean.insert.VEH_IS_CHECKPASS;
import org.bzjj.bean.insert.VEH_IS_PRINTQUEUE;
import org.bzjj.sql.OracleCon;
import org.bzjj.sql.Sql;
import org.bzjj.sql.SqlInsert;
import org.bzjj.sql.SqlSelect;
import org.bzjj.sql.SqlUpdate;
import org.bzjj.tool.ChatMethod;
import org.bzjj.tool.Tools;

public class CarResultUpdate extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String lsh = request.getParameter("lsh");
		// 查询mysql该信息是否处理过
		HashMap<String, String> where = new HashMap<String, String>();
		where.put("qqxlh", lsh);
		String rtn = Sql.executeQuery(SqlSelect.getSelect("process", "cjsj", where));

		if (rtn != null) {// 已处理过
			long t1 = Tools.dateFormat(rtn).getTime();
			long t2 = new Date().getTime();
			if ((t2 - t1) / (1000 * 60) < 10) {// 处理不到10分钟
				ChatMethod.setResponse(response, "该信息已经处理过，请10分钟之后再试！");
				return;
			}
		} // 未处理，或者已处理，处理间隔时间大于等于10分钟以上

		if (VEH_IS_PRINTQUEUE.exist(lsh)) {// 已生成打印流水
			ChatMethod.setResponse(response, "该信息无需处理！");
			return;
		}
		// 备份语句
		String path = "d:\\log\\" + "checkpass_" + lsh + ".sql";
		String instr = VEH_IS_CHECKPASS.getInsert(lsh);
		if (rtn == null) {// 第一次时备份
			Tools.saveSql(path, instr);
		}

		// 处理信息写入mysql
		HashMap<String, String> value = new HashMap<String, String>();
		value.put("qqxlh", lsh);
		if (rtn == null) {// 第一次插入
			Sql.executeUpdate(SqlInsert.getInsert("process", value));
		} else {// 以后更新时间
			HashMap<String, String> update = new HashMap<String, String>();
			update.put("cjsj", Tools.dateFormat1(new Date()));
			Sql.executeUpdate(SqlUpdate.getUpdate("process", update, value));
		}

		Connection connection = OracleCon.getDBonnect().getConnection();
		// oracle中删除该条信息
		Sql.executeUpdate(connection, "delete from veh_is_checkpass where lsh=" + lsh);
		// oracle中插入该条信息
		Sql.executeUpdate(connection, instr.substring(0, instr.length() - 1));

		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 返回处理结果,成功返回1，失败返回提示信息
		ChatMethod.setResponse(response, "1");

	}
}
